package com.ijiuchuang.jc_localization.tools.adapter.list;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.ijiuchuang.jc_localization.tools.adapter.ViewHolder;

import java.util.List;

public abstract class CommonAdapter<T> extends BaseAdapter
{
	protected Context mContext;
	protected List<T> mDatas;
	protected LayoutInflater mInflater;
	private int layoutId;

	public CommonAdapter(Context context,int layoutId, List<T> datas )
	{
		this.mContext = context;
		mInflater = LayoutInflater.from(context);
		this.mDatas = datas;
		this.layoutId = layoutId;
	}

	/**
	 * 判断数据是否为空
	 *
	 * @return 为空返回true，不为空返回false
	 */
	public boolean isEmpty() {
		return mDatas.isEmpty();
	}

	/**
	 * 在原有的数据上添加新数据
	 *
	 * @param itemList
	 */
	public void addItems(List<T> itemList) {
		this.mDatas.addAll(itemList);
		notifyDataSetChanged();
	}

	/**
	 * 设置为新的数据，旧数据会被清空
	 *
	 * @param itemList
	 */
	public void setItems(List<T> itemList) {
		this.mDatas.clear();
		this.mDatas = itemList;
		notifyDataSetChanged();
	}

	/**
	 * 清空数据
	 */
	public void clearItems() {
		mDatas.clear();
		notifyDataSetChanged();
	}

	@Override
	public int getCount()
	{
		return mDatas.size();
	}

	@Override
	public T getItem(int position)
	{
		return mDatas.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder = ViewHolder.get(mContext, convertView, parent,
				layoutId, position);
		convert(holder, getItem(position));
		return holder.getConvertView();
	}

	public abstract void convert(ViewHolder holder, T t);

}
